//
//  LGTTaskDescAlertVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/19.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTTaskDescAlertVC: LGTBaseVC {

    var tip:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = BlackAlphaColor
        let backView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(10))
        self.view.addSubview(backView)
        backView.isUserInteractionEnabled = true
        backView.backgroundColor = UIColor.white
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.equalTo(SizeFit(303))
            make.height.equalTo(SizeFit(380))
        }
        
        let textView:UITextView = UITextView.init()
        textView.font = KFont(size: SizeFitFloat(12))
        textView.textColor = TextColor
     
        //段落样式
        let paramStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.init()
        paramStyle.lineSpacing = SizeFitFloat(5)
        paramStyle.paragraphSpacing = SizeFitFloat(3)
//        let attr:NSAttributedString = NSAttributedString.init(string: self.tip ?? "", attributes: [NSAttributedString.Key.paragraphStyle: paramStyle,
//NSAttributedString.DocumentReadingOptionKey:NSAttributedString.DocumentType.html])
        
        //let attr:
        
//        let text = "1、仔细阅读任务要求，确保能按要求完成， 做任务前先点击任务页面下“立即申请”按钮， 先申请后做任务。</br> 2、根据任务步骤做要求完成任务，并提交验 证图或提交验证数据。</br> 3、在提交任务信息时，请检查上传验证图及 验证数据，确认后点击页面下“提交”按钮， 等待审核及领取奖励。</br> <font color='#F53A3A'size='20'>4、转账、投资理财的任务不能做，否则亏钱自负！</font>"
        
        let data = (self.tip ?? "").htmlToAttributedString
        textView.attributedText = data
        
        //textView.text = self.tip
        backView.addSubview(textView)
        textView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(SizeFit(252))
            make.height.equalTo(SizeFit(180))
            make.top.equalTo(SizeFit(68))
        }
        
        let titleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: "任务提示")
        backView.addSubview(titleLabel)
        titleLabel.font = UIFont.boldSystemFont(ofSize: SizeFitFloat(18))
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(22))
        }
        
        let clostBtn = LGTViewCreateUtil.share.createLGTButton(image: "close_icon", selected: "close_icon", dW: SizeFitFloat(10), dH: SizeFitFloat(10))
        clostBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        backView.addSubview(clostBtn)
        clostBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFit(13))
            make.top.equalTo(SizeFit(23))
            make.right.equalTo(-SizeFit(24))
        }
        
        //确认
        let sureButton:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "去做任务", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, corner:SizeFitFloat(20), backColor: YellowColor)
        sureButton.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        backView.addSubview(sureButton)
        sureButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(SizeFit(40))
            make.width.equalTo(SizeFit(252))
            make.top.equalTo(SizeFit(308))
        }
        
        let button:UIButton = LGTViewCreateUtil.share.createLGTButton(image: "unchecked", selected: "selection", dW: SizeFitFloat(10), dH: SizeFitFloat(10))
        backView.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.left.equalTo(sureButton)
            make.width.height.equalTo(SizeFit(16))
            make.top.equalTo(SizeFit(265))
        }
        button.addTarget(self, action: #selector(tipSelectAction), for: UIControl.Event.touchUpInside)
        
        let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: GrayColor, fontSize: SizeFitFloat(14), text: "不再提示")
        backView.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(button).offset(SizeFit(24))
            make.centerY.equalTo(button)
            make.height.equalTo(SizeFitFloat(14))
        }
    }
    
    @objc func closeAction(){
        self.dismiss(animated: false, completion: nil)
    }
    
    
    @objc func tipSelectAction(sender:UIButton){
        sender.isSelected = !sender.isSelected
        LGTUserUtil.share.setTaskTip(state: sender.isSelected)
    }
}


extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}
